/**
 * Projet : FoireAuxFiches
 * Fichier: faf.business.tests.UserManagerTests.java
 *
 * Pierre-olivier Pignon le 11 mars 2011 a 12:44:06 
 * 
 */
package faf.web;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import faf.business.UserManager;
import faf.entities.Account;
import faf.web.data.UpdateAccountForm;


/**
 * Controller pour le formulaire de mise a jour de profil
 * @author Pierre-Olivier Pignon
 * @date 11 mars 2011 a 14:10:16
 *
 */
public class UpdateAccountController extends SimpleFormController {
	
	UserManager userManager;

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	
	protected Object formBackingObject(HttpServletRequest request)
			throws ServletException {
		long account_id=0;
		Account account;
		UpdateAccountForm form = new UpdateAccountForm();
		Account loggedAccount = (Account) request.getSession().getAttribute("user");
		boolean canChangePwd =false;
		try {
			account_id = Long.parseLong(request.getParameter("account_id"));
			account = userManager.findAccount(account_id);
			if ((loggedAccount!=null)&&(loggedAccount.isAdministrator())){
				canChangePwd=true;
				System.out.println(account.getId()+" est admin");
			}
		} catch (NumberFormatException nfE) {
			account=loggedAccount;
			if (account!=null)
				canChangePwd=true;
			
		}
		
		if (account != null) {
			request.getSession().setAttribute("account_id", account.getId());
			form.setEmail(account.getMail());
			form.setFirstname(account.getFirstname());
			form.setLastname(account.getLastname());
		}
		
		request.getSession().setAttribute("canChangePwd", canChangePwd);
		return form;
	}

	public ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		long account_id=0;
		Account loggedAccount = (Account) request.getSession().getAttribute("user");
		Account account = null;
		try {
			account_id = Long.parseLong(request.getParameter("account_id"));
			account = userManager.findAccount(account_id);
		} catch (NumberFormatException nfE) {
			if (loggedAccount==null)
				return new ModelAndView(new RedirectView(getSuccessView()));
			account = loggedAccount;
		}
		UpdateAccountForm form = (UpdateAccountForm) command;
		String strMail = account.getMail();
		if ((userManager.findAccountByEmail(form.getEmail())!=null) && (!strMail.equalsIgnoreCase(form.getEmail()))){
			errors.rejectValue("email", "email-conflict");
			return showForm(request, response, errors);
		}
		else {
			account.setMail(form.getEmail());
			
		}
		account.setFirstname(form.getFirstname());
		account.setLastname(form.getLastname());
		userManager.updateAccount(account);
		return new ModelAndView(new RedirectView(getSuccessView()));
		
	}
}
